package com.manbearpig.todue;

import java.util.Date;
import com.manbearpig.todue.ToDo.Category;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.location.Location;
import android.widget.Toast;

/**
 * Manages the creation of todos
 * @author Brandon Devereaux
 * @version 1.0
 */
public class ToDo_Manager {
	private Filter filter;
	private String username;
	private String description;
	private String short_description;
	private Location location;
	private boolean recurrence;
	private Date date;
	private String category;
	private String completed;
		
	/**
	 * Returns a cursor retrieving the todos
	 * This method is not used
	 * @param username
	 * @param c
	 * @return
	 */
	public Cursor retrieveToDos(String username, Context c)
	{
		DatabaseHelper dbh = new DatabaseHelper(c);
        SQLiteDatabase db = dbh.getReadableDatabase();
        db.beginTransaction();
        Cursor cursor = db.rawQuery("SELECT _id, Description, Due_Date FROM Todos WHERE Username=?",new String[]{username});
        db.setTransactionSuccessful();
        db.close();
        dbh.close();
        return cursor;
	}
	/**
	 * Creates ToDo item if all parameters are correct.
	 * 
	 *
	 */
	public boolean attemptCreateToDo(Context c)
	{
		int recur;
		if(this.recurrence)
			recur = 1;
		else
			recur = 0;
		ToDo todo = new ToDo(this.date, this.location, this.description, this.short_description, recur, this.category, this.completed);
    	if(todo.isValid())
    	{
	    	todo.createToDo(this.username, c);
	    	return true;
    	}
    	else
    		this.popupError("One or more fields are invalid", c);
    	return false;
	}
	/**
	 * This method attempts to edit the Todo
	 * @param c The requested context
	 * @param id the id of the todo to edit
	 * @return true if the edit was successful, false otherwise
	 */
	public boolean attemptEditToDo(Context c, int id)
	{
		int recur;
		if(this.recurrence)
			recur = 1;
		else
			recur = 0;
		ToDo todo = new ToDo(this.date, this.location, this.description, this.short_description, recur, this.category, this.completed);
    	if(todo.isValid())
    	{
	    	todo.editToDo(this.username, c, id);
	    	return true;
    	}
    	else
    		this.popupError("One or more fields are invalid", c);
    	return false;
		
	}
	
	/**
	 * Raises a Toast displaying the reason for the failed creation
	 * @param errorMessage The text to display in the Toast
	 * @param c the Context of the Android so an error message can be shown to the users
	 */
	private void popupError(String errorMessage, Context c)
	{
		Toast toast = Toast.makeText(c,errorMessage,Toast.LENGTH_LONG);
		toast.show();
	}
	public Filter getFilter() {
		return filter;
	}
	public void setFilter(Filter filter) {
		this.filter = filter;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getShort_description() {
		return short_description;
	}
	public void setShort_description(String short_description) {
		this.short_description = short_description;
	}
	public Location getLocation() {
		return location;
	}
	public void setLocation(Location location) {
		this.location = location;
	}
	public boolean isRecurrence() {
		return recurrence;
	}
	public void setRecurrence(boolean recurrence) {
		this.recurrence = recurrence;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public String getCategory() {
		return category;
	}
	public void setCategory(String category) {
		this.category = category;
	}
	public String getCompleted() {
		return completed;
	}
	public void setCompleted(String completed) {
		this.completed = completed;
	}
}
